import type { TreeSelectOption } from 'naive-ui'
import type { ConfigState } from '@/types/hooks/useEdit'
import type { OptionsType } from '@/types/hooks/editDia.ts'
import { useEdit } from '@/hooks/useEdit.ts'
import { createRules } from './rules.ts'
import { createFormItems } from './formItems.ts'
import { usePageConfig } from '@/views/setting/department/config'

export type ExtendedProps = {
  treeOptions: TreeSelectOption[] | Ref<TreeSelectOption[]>
}

export function useDeptEdit(options: OptionsType = {}, extended: ExtendedProps) {
  const pageConfig = usePageConfig()
  const config: ConfigState = {
    ...pageConfig.edit.default,
    props: {
      formItems: [],
      rules: createRules(),
    },
  }
  if (!config?.key) {
    config.key = pageConfig.KEY || 'id'
  }
  const res = useEdit({
    ...config,
    ...options,
  })
  res.setFormItems(createFormItems(extended))
  return {
    ...res,
  }
}
